package com.sskj.level.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.common.util.ClickUtil;
import com.sskj.level.R;
import com.sskj.level.R2;
import com.sskj.level.component.DaggerUserDataComponent;
import com.sskj.level.presenter.LevelFragmentPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.LevelCoinType;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 杠杆（合约）交易主页面
 */
@Route(path = RConfig.LEVEL_FRAGMENT_MAIN)
public class LevelFragment extends BaseFragment<LevelFragmentPresenter> {
    @Inject
    UserViewModel userViewModel;

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.ivTopRight)
    ImageView ivTopRight;

    @BindView(R2.id.tradeFrameLayout)
    FrameLayout tradeFrameLayout;
    @BindView(R2.id.tvAllTrade)
    TextView tvAllTrade;
    @BindView(R2.id.holderFrameLayout)
    FrameLayout holderFrameLayout;
    @BindView(R2.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @Autowired(required = true)
    String code;
    @BindView(R2.id.ivTopLeft)
    ImageView ivTopLeft;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.level_fragment_main;
    }

    @Override
    public LevelFragmentPresenter getPresenter() {
        return new LevelFragmentPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        tvTitle.setText(code.replace("_", "/").toUpperCase());
        mPresenter.getProduct(code);

        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);

            } else {
                userData = null;

            }
        });

        ClickUtil.click(ivTopRight, () -> {
            ARouter.getInstance().build(RConfig.HANG_MARKET)
                    .withString(Constans.CODE, code)
                    .withBoolean(Constans.IS_BIBI, false)
                    .navigation();
        });

        ClickUtil.click(ivTopLeft, () -> {
            ARouter.getInstance().build(RConfig.LEVEL_SLIDE)
                    .navigation();
        });

        ClickUtil.click(tvAllTrade, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(),userData)){
                return;
            }
            ARouter.getInstance().build(RConfig.LEVEL_TRADE).withString(Constans.CODE, code).navigation();
        });

        smartRefreshLayout.setRefreshHeader(new MaterialHeader(getActivity()));
        Fragment buyAndSellFragment = (Fragment) ARouter.getInstance()
                .build(RConfig.LEVEL_FRAGMENT_BUY_AND_SELL)
                .withString(Constans.CODE, code)
                .navigation();
        Fragment holderFragment = (Fragment) ARouter.getInstance()
                .build(RConfig.LEVEL_FRAGMENT_CURRENT_ENTRUST)
                .withString(Constans.STOCK_CODE, code)
                .navigation();
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.tradeFrameLayout, buyAndSellFragment);
        fragmentTransaction.add(R.id.holderFrameLayout, holderFragment);
        fragmentTransaction.show(buyAndSellFragment);
        fragmentTransaction.show(holderFragment);
        fragmentTransaction.commitNow();

        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            LiveDataBus.get().with(RxBusCode.LEVEL_FRESH).postValue(1);
            smartRefreshLayout.finishRefresh(3);
        });
    }

    @Override
    protected void initData() {
        LiveDataBus.get().with(RxBusCode.LEVEL_CHANGE_COIN, LevelCoinType.class)
                .observe(this, this::refresh);
    }

    public void refresh(LevelCoinType coinType) {
        code = coinType.getCode();
        tvTitle.setText(code.replace("_", "/").toUpperCase());
        mPresenter.getProduct(code);
    }
}
